home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / bother__ / cenvid.zip / CENVIDOS.ZIP / CMMTUTOR.DOC < prev    next >
Text File  |  1995-04-01  |  45KB  |  1,045 lines

  1.                     CEnvi Shareware Manual, Chapter 2:
  2.                            Cmm Language Tutorial
  3.  
  4.                    
  5.                      CEnvi unregistered version 2.00
  6.                              29 March 1995
  7.  
  8.                  Copyright 1993, Nombas, All Rights Reserved.
  9.           Published by Nombas, 64 Salem Street, MEDFORD MA 02155 USA
  10.           
  11.                           VOICE (617) 391-6595
  12.                           BBS   (617) 391-3718
  13.                           FAX   (617) 391-3842
  14.  
  15.          Thank you for trying this shareware version of CEnvi from Nombas.
  16.  
  17.                           _______
  18.                      ____|__     |                (R)
  19.                   --|       |    |-------------------
  20.                     |   ____|__  |  Association of
  21.                     |  |       |_|  Shareware
  22.                     |__|   o   |    Professionals
  23.                   -----|   |   |---------------------
  24.                        |___|___|    MEMBER
  25.  
  26.  
  27.  
  28. 2.  The Cmm Language: Tutorial for Non-C Programmers
  29.  
  30.           The information in this chapter is geared toward those who are
  31.           not familiar with the C programming language.  C programmers
  32.           should jump ahead to the next chapter: Cmm versus C.  This
  33.           section is an introduction to and description of the Cmm
  34.           programming language.
  35.  
  36.           If you can write a batch, script, or macro file, or if you can
  37.           remember what "y = x + 1" means from your algebra class, then
  38.           you're ready to take on Cmm.  Really.  Cmm contains only
  39.           variables, mathematics symbols (remember algebra), and these few
  40.           statements: IF, ELSE, DO, WHILE, FOR, SWITCH, CASE, BREAK,
  41.           DEFAULT, CONTINUE, GOTO, and RETURN.
  42.  
  43.           This section is an abbreviation of the Cmm Language Tutorial
  44.           chapter in the CEnvi registered manual.  The CEnvi registered
  45.           manual goes into much more depth, has many more examples, and
  46.           follows a step-by-step tutorial to create a simple text editor
  47.           with CEnvi.
  48.  
  49. 2.1.  Your first Cmm program
  50.  
  51.           Before going into a description of Cmm, let's first make sure
  52.           that CEnvi is working properly.  With a text editor (e.g., EDIT
  53.           for DOS, E for OS/2, or NOTEPAD for Windows) create the file
  54.           HELLO.CMM and enter this text:
  55.  
  56.               // Hello.cmm: My first Cmm program
  57.               Count = 1; /* Count is how many Cmm programs I've written */
  58.               printf("Hello world. This is my %dst Cmm program.\n",Count);
  59.               printf("Press any key to quit...");
  60.               getch();
  61.  
  62.           You have now written a Cmm program named "HELLO.CMM".  Don't be
  63.           concerned if you do not yet understand the HELLO.CMM program; it
  64.           should become clear as Cmm is defined.  Now execute this program
  65.           (for DOS or OS/2 you would enter "CENVID HELLO.CMM" or "CENVI2 HELLO.CMM"
  66.           and for Windows you need may use the File Manager and double-click 
  67.           on the file name).  You should get this output:
  68.  
  69.               Hello world. This is my 1st Cmm program.
  70.               Press any key to quit...
  71.  
  72.           If this program will execute, then you are ready to go on to
  73.           learn about Cmm.  If it did not execute then consult the CEnvi
  74.           installation section in the first chapter of this shareware
  75.           manual.
  76.  
  77. 2.2.  Cmm comments
  78.  
  79.           Comments are used in Cmm code to explain what the code does, but
  80.           the comment itself does nothing.  Comments are very useful in
  81.           programming.  A comment takes nothing away from the execution of
  82.           a program, but adds immeasurably to the readability of the source
  83.           code.
  84.  
  85.           In Cmm, any text on a line following two slash characters (//) is
  86.           considered a comment and so is ignored by the Cmm interpreter.
  87.           Likewise, anything between a slash-asterisk (/*) and an
  88.           asterisk-slash (*/) is a comment (this type of comment may extend
  89.           over many lines).  In the HELLO.CMM program there is a "//"
  90.           comment on the first line and a "/* blah blah */" comment on the
  91.           second line.
  92.  
  93. 2.3.  Cmm primary data types
  94.  
  95.           There are three principal data types in Cmm:
  96.             *Byte: A character (e.g., 'D') or a whole number between 0 and
  97.               255, inclusive
  98.             *Integer: A whole number value; this is the most common numeric
  99.               data type (examples: 0, -1000, 567, 4335600)
  100.             *Float: floating point numbers; any number containing a decimal
  101.               point (examples: 0.567, 3.14159, -5.456e-12)
  102.  
  103.           Cmm determines the data type of a number by how it is used; for
  104.           example, in the HELLO.CMM program the "1" in the second line is
  105.           an integer because that is the default type for numbers without a
  106.           decimal point.
  107.  
  108. 2.3.1   Escape Sequences for Characters
  109.  
  110.           Certain characters are represented with a multi-character
  111.           sequence beginning with a backslash (\).  These are called escape
  112.           sequences, and have the following meanings:
  113.               \a      Audible bell
  114.               \b      Backspace
  115.               \f      Formfeed
  116.               \n      Newline
  117.               \r      Carriage return
  118.               \t      Tab
  119.               \v      Vertical tab
  120.               \'      Single quote
  121.               \"      Double quote
  122.               \\      Backslash character
  123.               \###    Octal number  // ex: '\033' is escape character
  124.                                     // ex: \0 is null character
  125.               \x##    Hex number    // '\x1B' is escape character
  126.  
  127. 2.4.  Cmm Variables
  128.  
  129.           A Cmm variable is a symbol that may be assigned data.  The
  130.           assignment of data is usually performed by the equal sign (=), as
  131.           in the line "Count = 1" in HELLO.CMM.  After variables have been
  132.           assigned, they can be treated as their data type.  So, after
  133.           these statements:
  134.               Count = 1               // assign count as the integer 1
  135.               Count = Count + 2       // same as: Count = 1 + 2
  136.           Count would now have the value 3.
  137.  
  138. 2.5.  Cmm Expressions, Statements, and Blocks
  139.  
  140.           A Cmm "expression" or "statement" is any sequence of code that
  141.           perform a computation or take an action (e.g., "Count=1",
  142.           "(2+4)*3").  Cmm code is executed one statement at a time in the
  143.           order it is read.  A Cmm program is a series of statements
  144.           executed sequentially, one at a time.  Each line of HELLO.CMM,
  145.           for example, follows the previous line as it is written and as it
  146.           is executed.
  147.  
  148.           A statement usually ends in a semicolon (;) (this is required in
  149.           C, and still a good idea in Cmm to improve readability).  Each
  150.           program statement is usually written on a separate line to make
  151.           the code easy to read.
  152.  
  153.           Expressions may be grouped to effect the sequence of processing;
  154.           expressions inside parentheses are processed first.  Notice that:
  155.               4 * 7 - 5 * 3       // 28 - 15 = 13
  156.           has the same meaning, due to algebraic operator precedence, as:
  157.               (4 * 7) - (5 * 3)   // 28 - 15 = 13
  158.           but has a different meaning than:
  159.               4 * (7 - 5) * 3     // 4 * 2 * 3 = 8 * 3 = 24
  160.           which is still different from:
  161.               4 * (7 - (5 * 3))   // 4 * (7 - 15) = 4 * -8 = -32
  162.  
  163.           A "block" is a group of statements enclosed in curly braces ({})
  164.           to show that they are all a group and so are treated as one
  165.           statement.  For example, HELLO.CMM may be rewritten as:
  166.               // Hello.cmm: My first Cmm program
  167.               Count = 1; /* Count is how many Cmm programs I've written */
  168.               printf("Hello world. This is my %dst Cmm program.\n",Count);
  169.               {
  170.                 // this block tells the user we're done, and quits
  171.                 printf("Press any key to quit...");
  172.                 getch();
  173.               }
  174.           The indentation of statements is not necessary, but is useful for
  175.           readability.
  176.  
  177. 2.6.  Cmm Mathematical Operators
  178.  
  179.           Cmm code usually contains some mathematical operations, such as
  180.           adding numbers together, multiplying, dividing, etc.  These are
  181.           written in a natural way, such as "2 + 3" when you want to add
  182.           two and three.  The next few subsections define the recognized
  183.           operators within the comments of sample code:
  184.  
  185. 2.6.1   Basic Arithmetic
  186.  
  187.               //  "="  assignment: sets a variable's value
  188.               i = 2;      // i is 2
  189.  
  190.               //  "+"  addition: adds two numbers
  191.               i = i + 3;  // i is now (2+3) or 5
  192.  
  193.               //  "-"  subtraction: subtracts a number
  194.               i = i - 3;  // i is now (5-3) or 2 again
  195.  
  196.               //  "*"  multiplication:
  197.               i = i * 5;  // i is now (2*5) or 10
  198.  
  199.               //  "/"  division:
  200.               i = i / 3;  // i is now (10/3) or 3 (no remainder)
  201.  
  202.               //  "%"  remainder: remainder after division
  203.               i = 10;
  204.               i = i % 3;  // i is now (10%3) or 1
  205.  
  206. 2.6.2   Assignment Arithmetic
  207.  
  208.           Each of the above operators can combined with the assignment
  209.           operator (=) as a shortcut.  This automatically assumes that the
  210.           assigned variable is the first variable in the arithmetic
  211.           operation.  Using assignment arithmetic operators, the above code
  212.           could be simplified as follows:
  213.  
  214.               //  "="  assignment: sets a variable's value
  215.               i = 2;      // i is 2
  216.  
  217.               //  "+="  assign addition: adds number
  218.               i += 3;     // i is now (2+3) or 5
  219.  
  220.               //  "-="  assign subtraction: subtracts a number
  221.               i -= 3;     // i is now (5-3) or 2 again
  222.  
  223.               //  "*="  assign multiplication:
  224.               i *= 5;     // i is now (2*5) or 10
  225.  
  226.               //  "/="  assign divide:
  227.               i /= 3;     // i is now (10/3) or 3 (no remainder)
  228.  
  229.               //  "%="  assign remainder: remainder after division
  230.               i = 10;
  231.               i %= i % 3; // i is now (10%3) or 1
  232.  
  233. 2.6.3   Auto-Increment (++) and Auto-Decrement (--)
  234.  
  235.           Other arithmetic shortcuts are the auto-increment (++) and
  236.           auto-decrement (--) operators.  These operators add or subtract 1
  237.           (one) from the value to which they are applied.  ("i++" is a
  238.           shortcut for "i+=1", which is itself shortcut for "i=i+1").
  239.           These operators can be used before (prefix) or after (postfix)
  240.           their variables.  If used before, then the variable is altered
  241.           before it is used in the statement; if used after, then the
  242.           variable is altered after it is used in the statement.  This is
  243.           demonstrated by the following code sequence:
  244.  
  245.               i = 4;    // i is initialized to 4
  246.               j = ++i;  // j is 5, i is 5 (i was incremented before use)
  247.               j = i++;  // j is 5, i is 6 (i was incremented after use)
  248.               j = --i;  // j is 5, i is 5 (i was decremented before use)
  249.               j = i--;  // j is 5, i is 4 (i was decremented after use)
  250.  
  251. 2.7.  Cmm Arrays and Strings
  252.  
  253.           An "array" is a group of individual data elements.  Each
  254.           individual item in the array is then called an "array element".
  255.           An element of an array is itself a variable, much like any other
  256.           variable.  Any particular element of the array is selected by
  257.           specifying the element's offset in the array.  This offset is an
  258.           integer in square brackets ([]).  For example:
  259.  
  260.               prime[0] = 2;
  261.               prime[1] = 3;
  262.               prime[2] = 5;
  263.               month[0] = "January";
  264.               month[1] = "February";
  265.               month[2] = "March";
  266.  
  267.           An array in Cmm does not need to be pre-defined for size or data
  268.           type, as it does in other languages.  Any array extends from
  269.           minus infinity to plus infinity (within reasonable computer
  270.           memory limits).  The data type for the array is the type of the
  271.           data first assigned to it.
  272.  
  273.           If Cmm code begins with:
  274.               foo[5] = 7;
  275.               foo[2] = -100;
  276.               foo[-5] = 4;
  277.           then foo is an array of integers and the element at index 7 is a
  278.           5, at index 2 is -100, and at index -5 is 4.  "foo[5]" can be
  279.           used in the code anywhere that a variable could be used.
  280.  
  281. 2.7.1   Array Initialization
  282.  
  283.           Arrays can be initialized by initializing specific elements, as
  284.           in:
  285.               foo[5] = 7; foo[2] = -100; foo[-5] = 4;
  286.           or by enclosing all the initial statements in curly braces, which
  287.           will cause the array to start initializing at element 0, as in:
  288.               foo = { 0, 0, -100, 0, 0, 7 };
  289.  
  290. 2.7.2   Strings
  291.  
  292.           Strings are arrays of bytes that end in the null-byte (zero).
  293.           "String" is just a shorthand way to specify this array of bytes.
  294.           A string is most commonly specified simply by writing text within
  295.           two quote characters (e.g., "I am a string.")
  296.  
  297.           If this statement were present in the Cmm code:
  298.               animal = "dog";
  299.           it would be identical to this statement:
  300.               animal = { 'd', 'o', 'g', 0 };
  301.           and in both cases the value at animal[0] is 'd', at animal[2] is
  302.           'g', and at animal[3] is 0.
  303.  
  304.           Escape sequences encountered in strings will be translated into
  305.           their byte value.  So you'll often see strings such as "\aHello
  306.           world\n" where the "\a" means to beep and the "\n" means to put a
  307.           newline character at the end of the string.  Cmm provides the
  308.           back-quote (`), also known as known as the "back-tick" or "grave
  309.           accent", as an alternative quote character to indicate that
  310.           escape sequences are not to be translated.  So, for example, here
  311.           are two ways to describe the same file name:
  312.               "c:\\autoexec.bat"    // traditional method
  313.               `c:\autoexec.bat`   // alternative method
  314.  
  315. 2.7.3   Array Arithmetic
  316.  
  317.           When one array is assigned to the other, as in:
  318.               foo = "cat";
  319.               goo = foo;
  320.           then both variables define the same array and start at the same
  321.           offset 0.  In this case, if foo[2] is changed then you will find
  322.           that goo[2] has also been changed.
  323.  
  324.           Integer addition and subtraction can also be performed on arrays.
  325.           Array addition or subtraction sets where the array is based.  By
  326.           altering the previous code segment to:
  327.               foo = "cat";
  328.               goo = foo + 1;
  329.           goo and foo would now be arrays containing the same data, except
  330.           that now goo is based one element further, and foo[2] is now the
  331.           same data as goo[1].
  332.  
  333.           To demonstrate:
  334.               foo = "cat";    // foo[0] is 'c', foo[1] = 'a'
  335.               goo = foo + 1;// goo[0] is 'a', goo[-1] = 'c'
  336.               goo[0] = 'u'; // goo[0] is 'u', foo[1] = 'u', foo is "cut"
  337.               goo++;        // goo[0] is 't', goo[-2] = 'c'
  338.               goo[-2] = 'b' // goo[0] is 't', foo[0] = 'b', foo is "but"
  339.  
  340. 2.7.4   Multi-Dimensional Arrays: Arrays of Arrays
  341.  
  342.           An array element is a variable, and so if the type of that
  343.           element's variable is itself an array, then you have an array of
  344.           arrays.  A statement such as:
  345.               goo[4][2] = 5;
  346.           indicates that goo is an array of arrays, and that element 2 of
  347.           element 4 is the integer 5.
  348.  
  349.           Multi-dimensional arrays might be useful for programming a
  350.           tic-tac-toe game.  Each row and column of the 3x3 board could be
  351.           represented with a row, column element containing 'X', 'O' or ' '
  352.           (blank) depending on the character at that location.  For
  353.           example, a horizontal 'X' win in the middle row could be
  354.           initialized like this:
  355.               board[1][0] = 'X';
  356.               board[1][1] = 'X';
  357.               board[1][2] = 'X';
  358.  
  359.           Note that a string is an array, and so anytime you make an array
  360.           of strings you are defining an array of arrays.
  361.  
  362. 2.8.  Cmm Structures
  363.  
  364.           A "structure" is a collection of named variables that belong
  365.           together as a whole.  Each of the named variables in a structure
  366.           is called a member of that structure and can be any data type
  367.           (integer, float, array, another structure, array of structures,
  368.           etc.).  These structure members are associated with the structure
  369.           by using a period between the structure variable and the member
  370.           name.
  371.  
  372.           A simple and useful example of a structure is to specify a point
  373.           on the screen.  A point is made up of a row and column, and so
  374.           you may specify:
  375.               place.row = 12;   // set to row 12
  376.               place.col = 20;   // set at column 20
  377.               place.row--;        // move up one row to row 11
  378.  
  379.           Two structures can be compared for equality or inequality, and
  380.           they may be assigned, but no other arithmetic operations can be
  381.           performed on a structure.
  382.  
  383. 2.9.  printf() and getch()
  384.  
  385.           Although although the Cmm "function" has not yet been defined, it
  386.           is useful to use two functions already, printf() and getch(), for
  387.           learning more about Cmm programming.  Unfortunately, printf() is
  388.           about as complicated as a function can get.
  389.  
  390. 2.9.1   printf() Syntax
  391.  
  392.           printf() prints to the screen, providing useful feedback on your
  393.           programming.  The basic syntax of printf() is this:
  394.  
  395.               printf(FormatString,data1,data2,data3,...);
  396.  
  397.           FormatString is the string that will be printed to the screen.
  398.           When FormatString contains a percent character (%) followed by
  399.           another character, then instead of printing those characters
  400.           printf() will print the next data item (data1, data2, data3,
  401.           etc...).  The way the data will be presented is determined by the
  402.           characters immediately following the percent sign (%).  There are
  403.           many such data formats, as described fully in the Registered
  404.           User's Manual (or any C programming manual), but here's few to
  405.           start with:
  406.             *%%  print a percentage sign
  407.             *%d  print a decimal integer
  408.             *%X  print a hexadecimal integer
  409.             *%c  print a character
  410.             *%f  print a floating-point value
  411.             *%E  print a floating-point value in scientific format
  412.             *%s  print a string
  413.  
  414.           There are also special characters that cause unique screen
  415.           behavior when they are printed, some of which are:
  416.             *\n  goto beginning of next line
  417.             *\t  tab
  418.             *\a  alarm: audible beep
  419.             *\r  carriage-return without a line feed
  420.             *\"  print the quote character
  421.             *\\  print a backslash
  422.  
  423.           Here are some example printf() statements:
  424.               printf("Hello world. This is my %dst Cmm program.\n",Count);
  425.               printf("%c %d %dlips",'I',8,2);
  426.               printf("%d decimal is the same as %X in hexadecimal",n,n);
  427.               printf("My name is: %s\n","Mary");
  428.  
  429. 2.9.2   getch() Syntax
  430.  
  431.           getch() (GET CHaracter) waits for any key to be pressed.  The
  432.           program stops executing at the getch() statement until a key is
  433.           pressed.  getch() is especially useful in Windows because once a
  434.           program is completed its windows will go away.  So getch() might
  435.           be placed at the end of the program to keep the screen visible
  436.           until a key is pressed.
  437.  
  438. 2.9.3   Using printf() and getch() to Learn Cmm
  439.  
  440.           You can now experiment with what you are learning by using the
  441.           printf() and getch() statements.
  442.  
  443.           If, during program execution, you want to know that you're about
  444.           to execute an exciting piece of code, then before those
  445.           interesting lines of code you may want to write:
  446.  
  447.               printf("Here comes the good stuff.  Press any key...");
  448.               getch();
  449.  
  450.           You can display values at any point in your program with a
  451.           printf() statement.  This short TEST.CMM program tests how "*="
  452.           works:
  453.  
  454.               // TEST.CMM - test the *= operator
  455.               var1 = 4;
  456.               var2 = 5;
  457.               printf("the numbers are %d and %d\n",var1,var2);
  458.               var1 *= 7;
  459.               var2 *= 7;
  460.               printf("after *= 7 the values are %d and %d\n",var1,var2);
  461.               getch();
  462.  
  463. 2.10. Bit Operators
  464.  
  465.           Cmm contains many operators for operating on the binary bits in a
  466.           byte or an integer.  If you're not familiar with hexadecimal
  467.           numbering (digits '0' to 'F'), you may want to refer to the CEnvi
  468.           Registered User's Manual.
  469.  
  470.               i = 0x146;
  471.  
  472.               //  "<<"  shift left: shift all bits left by value
  473.               //  "<<=" assignment shift left
  474.               i <<= 2;    // shift i (0x146) left 2, so i = 0x518
  475.  
  476.               //  ">>"  shift right: shift all bits right by value
  477.               //  ">>=" assignment shift right
  478.               i >>= 3;    // shift i (0x518) right 3, so i = 0xA3
  479.  
  480.               //  "&"   bitwise AND
  481.               //  "&="  assignment bitwise and
  482.               i &= 0x35;  // and i (0xA3) with 0x35 so i = 0x21
  483.  
  484.               //  "|"   bitwise OR
  485.               //  "|="  assignment bitwise OR
  486.               i |= 0xC5;    // OR i (0x21) with 0xC4 so i = 0xE5
  487.  
  488.               //  "^"   bitwise XOR: exlusive OR
  489.               //  "^="  assignment bitwise XOR
  490.               i ^= 0xF329 // XOR i (0xE5) with so i = 0xF3CC
  491.  
  492.               //  "~"   Bitwise complement: (turn 0 bits to 1, and 1 to 0)
  493.               i = ~i;     // complement i (0xF3CC) so i = 0xFFFF0C33 (OS/2)
  494.  
  495. 2.11. Logical Operators and Conditional Expressions
  496.  
  497.           A conditional expression is evaluated to be TRUE or FALSE, where
  498.           FALSE means zero, and TRUE means anything that is not FALSE
  499.           (i.e., not zero).  A variable or any other expression by itself
  500.           can be TRUE or FALSE (i.e., non-zero or zero).  With logic
  501.           operators, these expressions can be combined to make encompassing
  502.           TRUE/FALSE decisions.  The logic operators are:
  503.  
  504.             * "!"   NOT: opposite of decision
  505.               !TRUE               // FALSE
  506.               !FALSE              // TRUE
  507.               !('D')              // FALSE
  508.               !(5-5)              // TRUE
  509.  
  510.             * "&&"  AND: TRUE if and only if both expressions are true
  511.               TRUE && FALSE       // FALSE
  512.               TRUE && TRUE        // TRUE
  513.               0 && (foo+2)        // FALSE: (foo+2) is not evaluated
  514.               !(5-5) && 4         // TRUE
  515.  
  516.             * "||"  OR: TRUE if either expression is TRUE
  517.               TRUE || FALSE       // TRUE: doesn't even check FALSE
  518.               TRUE || (4+2)       // TRUE: (4+2) is not evaluated
  519.               FALSE || (4+2)      // TRUE: (4+2) is evaluated
  520.               FALSE || FALSE      // FALSE
  521.               0 || 0.345          // TRUE
  522.               !(5-3) || 4         // TRUE
  523.  
  524.             * "=="  EQUALITY: TRUE if both values equal, else FALSE
  525.               5 == 5              // TRUE
  526.               5 == 6              // FALSE
  527.  
  528.             * "!="  INEQUALITY: TRUE if both values not equal, else FALSE
  529.               5 != 5              // FALSE
  530.               5 != 6              // TRUE
  531.  
  532.             * "<"   LESS THAN
  533.               5 < 5               // FALSE
  534.               5 < 6               // TRUE
  535.  
  536.             * ">"   GREATER THAN
  537.               5 > 5               // FALSE
  538.               5 > 6               // FALSE
  539.               5 > -1              // TRUE
  540.  
  541.             * "<="  LESS THAN OR EQUAL TO
  542.               5 <= 5              // TRUE
  543.               5 <= 6              // TRUE
  544.               5 <= -1             // FALSE
  545.  
  546.             * ">="  GREATER THAN OR EQUAL TO
  547.               5 >= 5              // TRUE
  548.               5 >= 6              // FALSE
  549.               5 >= -1             // TRUE
  550.  
  551. 2.12. Flow Decisions: Loops, Conditions, and Switches
  552.  
  553.           This section describes how Cmm statements can control the flow of
  554.           your program.  You'll notice code sections are often indented,
  555.           when blocks belong together; this makes the code much easier to
  556.           read.
  557.  
  558. 2.12.1  IF - Execute statement (or block) if conditional expression is TRUE
  559.  
  560.               if ( goo < 10 ) {
  561.                 printf("goo is smaller than 10\n");
  562.               }
  563.  
  564. 2.12.2  ELSE - Execute statement (or block) if IF block was not executed
  565.               if ( goo < 10 ) {
  566.                 printf("goo is smaller than 10\n");
  567.               } else {
  568.                 printf("goo is not smaller than 10\n");
  569.               }
  570.  
  571.           ELSE can also be combined with IF to find one in a series of
  572.           values:
  573.  
  574.               if ( goo < 10 ) {
  575.                 printf("goo is less than 10\n");
  576.                 if ( goo < 0 ) {
  577.                   printf("goo is negative; of course it's less than 10\n");
  578.                 }
  579.               } else if ( goo > 10 ) {
  580.                 printf("goo is greater than 10\n");
  581.               } else {
  582.                 printf("goo is 10\n");
  583.               }
  584.  
  585. 2.12.3  WHILE - Execute block while conditional expression is TRUE
  586.  
  587.               str = "WHO ARE YOU?";
  588.               while ( str[0] != 'A' ) {
  589.                 printf("%s\n",str);
  590.                 str++;
  591.               }
  592.               printf("%s\n",str);
  593.  
  594.           The output of the above code would be:
  595.               WHO ARE YOU?
  596.               HO ARE YOU?
  597.               O ARE YOU?
  598.                ARE YOU?
  599.               ARE YOU?
  600.  
  601. 2.12.4  DO ... WHILE - Execute block, and then test for conditional
  602.  
  603.           This is different from the WHILE statement in that the block is
  604.           executed at least once, before the condition is tested.
  605.  
  606.               do {
  607.                 value++;
  608.                 printf("value = %d\n",value);
  609.               } while( value < 100 );
  610.  
  611.           The code used to demonstrate the WHILE statement might be better
  612.           written this way to avoid the extra printf():
  613.  
  614.               str = "WHO ARE YOU?";
  615.               do {
  616.                 printf("%s\n",str);
  617.               } while ( str++[0] != 'A' );
  618.  
  619. 2.12.5  FOR - initialize, test conditional, then loop
  620.  
  621.           "for" is a combination of statements of this format:
  622.               for ( initialization; conditional; loop_expression )
  623.                 statement
  624.           The initialization is performed first.  Then the conditional is
  625.           tested.  If the conditional is TRUE (or if there is no
  626.           conditional expression) then statement is executed, and then the
  627.           loop_expression is executed, and so on back to testing the
  628.           conditional.  If the conditional is FALSE then the program
  629.           continues beyond statement.  The "for" statement is a shortcut
  630.           for this form of WHILE:
  631.               initialization;
  632.               while ( conditional ) {
  633.                 statement;
  634.                 loop_expression;
  635.               }
  636.  
  637.           The above code demonstrating the WHILE statement would be
  638.           rewritten this way if you preferred to use the FOR statement:
  639.  
  640.               for ( str = "WHO ARE YOU?"; str[0] != 'A'; str++ )
  641.                 printf("%s\n",str);
  642.               printf("%s\n",str);
  643.  
  644.  
  645. 2.12.6  BREAK and CONTINUE
  646.  
  647.           "break" terminates the nearest loop or case statement.
  648.  
  649.           "continue" jumps to the test condition in the nearest DO or WHILE
  650.           loop, and jumps to the loop_expression in the nearest FOR loop.
  651.  
  652. 2.12.7  SWITCH, CASE, and DEFAULT
  653.  
  654.           The SWITCH statement follows this format:
  655.  
  656.               switch( switch_expression ) {
  657.                 case exp1:  statement1
  658.                 case exp2:  statement2
  659.                 case exp3:  statement3
  660.                 case exp4:  statement4
  661.                 case exp5:  statement5
  662.                 .
  663.                 .
  664.                 .
  665.                 default: default_statement
  666.               }
  667.  
  668.           switch_expression is evaluated, and then it is compared to all of
  669.           the case expressions (expr1, expr2, expr3, etc...) until a match
  670.           is found (i.e., switch_expression == expr? ).  The statements
  671.           following that match are then executed until the end of the
  672.           switch block is reached or until a BREAK statement exits the
  673.           switch block.  If no match is found, the DEFAULT: statement is
  674.           executed if there is one.
  675.  
  676.           For example, this code handles the variable "key", which is
  677.           assumed to be the value of a key that was just pressed on the
  678.           keyboard:
  679.  
  680.               switch ( key ) {
  681.                 case '0':
  682.                 case '1':
  683.                 case '2':
  684.                 case '3':
  685.                 case '4':
  686.                 case '5':
  687.                 case '6':
  688.                 case '7':
  689.                 case '8':
  690.                 case '9:
  691.                   printf("A digit was pressed\n");
  692.                   break;
  693.                 case '\r';
  694.                   printf("You pressed RETURN\n");
  695.                 default:
  696.                   printf("I am not prepared for the key you pressed\n");
  697.                   break;
  698.               }
  699.  
  700. 2.12.8  GOTO and Labels
  701.  
  702.           You may jump to any location within a function block (see
  703.           functions) by using the GOTO statement.  The syntax is:
  704.               goto LABEL;
  705.           where LABEL is an identifier followed by a colon (:).
  706.  
  707.               if ( a < 0 ) {
  708.                 BadGoingsOn:      // this is a label
  709.                 printf("\aThe value for a is very bad.\n");
  710.                 abort();          // see function library for abort()
  711.               } else if ( 1000 < a )
  712.                 goto BadGoingsOn;
  713.  
  714.           GOTOs should be used sparingly, for they often make it difficult
  715.           to track program flow.
  716.  
  717. 2.13. Conditional Operator ?:
  718.  
  719.           The conditional operator is a strange-looking statement that is
  720.           simply a useful shortcut.  The syntax is this:
  721.  
  722.               test_expression ? expression_if_true : expression_if_false
  723.  
  724.           First, test_expression is evaluated.  If test_expression is
  725.           non-zero (TRUE) then expression_if_true is evaluated and the
  726.           value of the entire expression replaced by the value of
  727.           expression_if_true.  If test_expression is FALSE, then
  728.           expression_if_false is evaluated and the value of the entire
  729.           expression is that of expression_if_false.
  730.  
  731.           For example:
  732.               foo = ( 5 < 6 ) ? 100 : 200;  // foo is set to 100
  733.               printf("User's name is %s\n",NULL==name ? "unknown" : name);
  734.  
  735. 2.14. Functions
  736.  
  737.           Functions in Cmm can perform any action of any complexity, and
  738.           yet they are used in statements as easily as variables.  It is
  739.           because of the flexibility and simplicity of functions that Cmm
  740.           needs so few statements.  Any action can take place in a
  741.           function, and yet the function is treated by the calling code
  742.           simply as the data type that the function returns.
  743.  
  744. 2.14.1  Function definition
  745.  
  746.           A function takes a form such as this:
  747.  
  748.               FunctionName(Parameter1,Parameter2,Parameter3)
  749.               {
  750.                 statements...
  751.                 return result;
  752.               }
  753.  
  754.           where the function name is followed by a list of input parameters
  755.           in parentheses (there can be any number of input parameters and
  756.           they can be named with any variable names).
  757.  
  758.           A call is made to a function in this format:
  759.  
  760.               FunctionName(Value1,Value2,Value3)
  761.  
  762.           So any call to FunctionName will be result in the execution of
  763.           FunctionName() with the parameters passed, and then be equivalent
  764.           to whatever value FunctionName() returns.
  765.  
  766. 2.14.2  Function RETURN Statement
  767.  
  768.           The return statement causes a function to return to the code that
  769.           initially called the function.  The calling code will receive the
  770.           value that the function returned.  Here are function examples:
  771.  
  772.               foo(a,b)    // return a times b
  773.               {
  774.                 return( a * b );
  775.               }
  776.  
  777.               foo(a,b)    // return a times b
  778.               {
  779.                 return a * b;
  780.               }
  781.  
  782.               foo(a,b)    // return the minimum value of a and b
  783.               {
  784.                 if ( a < b )
  785.                   result = a;
  786.                 else
  787.                   result = b;
  788.                 return(result);
  789.               }
  790.  
  791.               foo(a,b)    // return a structure with members .min and .max
  792.               {           // for the smaller and larger of a and b
  793.                 if ( a < b ) {
  794.                   bounds.min = a;
  795.                   bounds.max = b; 
  796.                 } else {
  797.                   bounds.min = b;
  798.                   bounds.max = a;
  799.                 }
  800.                 return( bounds );
  801.               }
  802.  
  803.           If no value is returned, or if the end of the function block is
  804.           reached without a return statement, then no value is returned and
  805.           the function is a "void" type.  void functions return no value to
  806.           the calling code.  These examples demonstrate some void-returning
  807.           functions:
  808.  
  809.               foo(a,b)    // set a = b squared. No return value.
  810.               {
  811.                 a = b * b;
  812.                 return;
  813.               }
  814.  
  815.               foo(a,b)    // set a = b squared. No return value.
  816.               {
  817.                 a = b * b;
  818.               }
  819.  
  820. 2.14.3  Function Example
  821.  
  822.           The use of functions should gain clarity with this example.  This
  823.           code demonstrates a function that multiplies two integers
  824.           (although multiplying integers could more easily be performed
  825.           with the multiply (*) operator):
  826.  
  827.               num1 = 4;
  828.               num2 = 6;
  829.  
  830.               // use the standard method of multiplying numbers
  831.               printf("%d times %d is %d\n",num1,num2,num1*num2);
  832.  
  833.               // now call our function to do the same thing
  834.               printf("%d times %d is %d\n",num1,num2,Multiply(num1,num2));
  835.  
  836.                 // declare a function that multiplies two integers.  Notice
  837.                 // that the integers are defined as i and j, so that's how
  838.                 // they'll be called in this function, no matter what they
  839.                 // were named in the calling code.  This function will
  840.                 // return i added to itself j times.
  841.               Multiply(i,j)
  842.               {
  843.                 total = 0;
  844.                 for ( count = 0; count < j; count++ )
  845.                   total += i;
  846.                 return(total);  // caller will receive this value
  847.               }
  848.  
  849.           When executed, the above code will print:
  850.               4 times 6 is 24
  851.               4 times 6 is 24
  852.  
  853.           This example demonstrated several features of Cmm functions.
  854.           Notice that in calling printf() the parameter "num1*num2" is not
  855.           passed to printf, but the result of "num1*num2" is passed to
  856.           printf.  Likewise, "Multiply(num1,num2)" is not a parameter to
  857.           printf(); instead, printf receives the return value of Multiply()
  858.           as its parameter.
  859.  
  860. 2.14.4  Function Recursion
  861.  
  862.           When a function calls itself, or calls some other function that
  863.           calls itself, then it is known as a recursive function.
  864.           Recursion is permitted in Cmm, as it is in C.  Each call into a
  865.           function is independent of any other call to that function (see
  866.           section on variable scope).
  867.  
  868.           The following function, factor(), factors a number.  Factoring is
  869.           a natural candidate for recursion because it is a repetitive
  870.           process where the result of one factor is then itself factored
  871.           according to the same rules.
  872.  
  873.               factor(i)   // recursive function to print all factors of i,
  874.               {           // and return the number of factors in i
  875.                  if ( 2 <= i ) {
  876.                     for ( test = 2; test <= i; test++ ) {
  877.                        if ( 0 == (i % test) ) {
  878.                           // found a factor, so print this factor then call
  879.                           // factor() recursively to find the next factor
  880.                           printf(" %d",test);
  881.                           return( 1 + factor(i/test) );
  882.                        }
  883.                     }
  884.                  }
  885.                  // if this point was reached, then factor not found
  886.                  return( 0 );
  887.               }
  888.  
  889. 2.15. Variable Scope
  890.  
  891.           A variable in Cmm is either global or local.  A global variable
  892.           is one that is referenced outside of any function, making it
  893.           visible and accessible to all functions.  A local variable is one
  894.           that is only referenced inside of a function, and so it only has
  895.           meaning and value within the function that declares it.  Note
  896.           that two identically-named local variables in different functions
  897.           are not the same variable.  Also, each instance of a recursive
  898.           function has its own set of local variables.  In other words, a
  899.           variable that is not referenced outside of a function only has
  900.           meaning (and that meaning is unique) while the code for that
  901.           function is executing.
  902.  
  903.           In the following sample code, "number" is the only global
  904.           variable, the two "result" variables in Quadruple() and Double()
  905.           are completely independent, and the "result" variable in one call
  906.           Double() is unique to each call to Double():
  907.  
  908.               number = Quadruple(3);
  909.  
  910.               Quadruple(i)
  911.               {
  912.                 result = Double(Double(i));
  913.                 return(result);
  914.               }
  915.  
  916.               Double(j)
  917.               {
  918.                 result = j + j;
  919.                 return(result);
  920.               }
  921.  
  922.           Note that variables that are all in uppercase letter are
  923.           considered global and refer to environment variables.  See the
  924.           chapter on CEnvi specifics for more on environment variables.
  925.  
  926. 2.16. #define
  927.  
  928.           "#define" is used to replace a token (a variable or a function
  929.           name) with other characters.  The structure is:
  930.               #define token replacement
  931.           This results in all subsequent occurrences of "token" to be
  932.           replaced with "replacement".  For example:
  933.               #define HI  "Hello world!"
  934.               printf(HI);
  935.           would result in the following output:
  936.               Hello world!
  937.           because HI was replaced with "Hello world!"
  938.  
  939.           A common use of #define is to define constant numeric or string
  940.           values that might possibly change, so that in the future only the
  941.           #define at the top of the file needs to be altered.  For
  942.           instance, if you write screen routines for a 25-line monitor, and
  943.           then later decide to make it a 50-line monitor, you're better off
  944.           altering
  945.               #define ROW_COUNT   25
  946.           to
  947.               #define ROW_COUNT   50
  948.           and using ROW_COUNT in your code (or ROW_COUNT-1, ROW_COUNT+2,
  949.           ROW_COUNT/2, etc...) than if you had to search for every instance
  950.           of the numbers 25, 24, 26, etc...
  951.  
  952.           CEnvi has a large default list of tokens that have already been
  953.           #define'd, such as TRUE, FALSE, and NULL.  More of these are
  954.           listed in the chapter on the Internal Function Library.
  955.  
  956. 2.17. #include
  957.  
  958.           Cmm code is executed from one stream of source code.  Sometimes
  959.           it is useful to break the program into different files or to
  960.           access commonly used code without recreating that code again and
  961.           again in every new program written.  This is what "#include" is
  962.           for: it allows code from another file to be included in a
  963.           program.
  964.  
  965.           The #include syntax is:
  966.  
  967.               #include <Name[,Ext[,Prefix[,BeginCode[,EndCode]]]]>
  968.               where:
  969.               Name - Name of the file to include
  970.               Ext - Extension name to add to Name if not already there
  971.               Prefix - Will only include code from Name on lines that begin
  972.                        with this string
  973.               BeginCode - Will start including code from Name following
  974.                           this line of text
  975.               EndCode - Will stop including code when this line is read
  976.  
  977.           The quote characters (' or ") may be used in place of < and >.
  978.           The only field that is required by Cmm is Name (this is the field
  979.           used in C's #include), which is the name of the file to include.
  980.           All other fields are optional, and can be left blank if unused.
  981.  
  982.           To include all of C:\CMM\LIB.cmm
  983.               #include <c:\CMM\LIB.cmm>
  984.           To include from dog.bat between the lines GOTO END and :END
  985.               #include 'dog,bat,,GOTO END,:END'
  986.           To include lots of little files into one big one program
  987.               #include <screen.cmm>
  988.               #include <keyboard.cmm>
  989.               #include <init.cmm>
  990.               #include <comm.cmm>
  991.  
  992.           In CEnvi a file will not be included more than once, and so if it
  993.           has already been included, a second (or third) #include statement
  994.           will have no effect.
  995.  
  996. 2.18. Initialization
  997.  
  998.           All code that is outside of any function is part of the global
  999.           initialization function.  This code is executed first, before
  1000.           calling any function (unless a function is called from this
  1001.           initialization code).
  1002.  
  1003.           Any variables referenced in the initialization section are
  1004.           global.
  1005.  
  1006. 2.19. main(ArgCount,ArgStrings)
  1007.  
  1008.           After the initialization has been performed, the main() function
  1009.           is called and is given the argument input to the Cmm program.
  1010.           The first parameter to main (ArgCount) is the number of arguments
  1011.           provided to the program.  ArgStrings is an array of those input
  1012.           arguments, which are always strings.  The first argument is
  1013.           always the name of the source code, or of CEnvi.exe if there is
  1014.           no source file.
  1015.  
  1016.           The value returned from main() should be an integer, and is the
  1017.           exit code (ERRORLEVEL for DOS) returned to the operating system
  1018.           from your program (see also exit() in the internal library).
  1019.  
  1020.           This code file TEST.CMM:
  1021.               printf("I am initializing\n");
  1022.               main(argc,argv)
  1023.               {
  1024.                 printf("This program is called %s.\n",argv[0]);
  1025.                 printf("There are %d input arguments, as follows:\n",argc);
  1026.                 for ( i = 0; i < argc; i++ )
  1027.                   printf("\t%s\n",argv[i]);
  1028.                 return(0);
  1029.               }
  1030.               printf("still initializing\n");
  1031.           When executed in this way:
  1032.               cenvi test.cmm I am 4 "years old" today
  1033.           would result in this output:
  1034.               I am initializing
  1035.               still initializing
  1036.               This program is called test.CMM.
  1037.               There are 6 input arguments, as follows:
  1038.                       test.CMM
  1039.                       I
  1040.                       am
  1041.                       4
  1042.                       years old
  1043.                       today
  1044.           and would return 0 to the operating system.
  1045.